﻿using System;
using System.Net.Sockets;

namespace Longbow.Net
{
    /// <summary>
    /// Socket 工厂类
    /// </summary>
    public static class SocketFactory
    {
        /// <summary>
        /// 创建Socket Client
        /// </summary>
        /// <param name="host">本地 IP 地址</param>
        /// <param name="port">本地 Port 端口，默认为0表示启用随机端口</param>
        /// <param name="configure">SocketConnectOptions 配置项回调函数</param>
        /// <returns>ISocketClient实例</returns>
        public static ISocketClient CreateClient(string host = null, int port = 0, Action<SocketConnectOptions> configure = null)
        {
            return new InternalSocketClient(host, port, configure);
        }

        /// <summary>
        /// 创建Socket Server
        /// </summary>
        /// <param name="host">绑定IP地址</param>
        /// <param name="port">绑定端口</param>
        /// <param name="configure">配置项</param>
        /// <returns>ISocketServer 实例</returns>
        public static ISocketServer CreateServer(string host = null, int port = 8010, Action<SocketServerOptions> configure = null)
        {
            return new InternalSocketServer(host, port, configure);
        }

        #region Internal static method
        /// <summary>
        /// SocketServer 接受客户端连接时创建SocketClient对象
        /// </summary>
        /// <param name="client">TcpClient 实例</param>
        /// <param name="configure">SocketConnectOptions 配置类回调函数</param>
        /// <returns></returns>
        internal static InternalSocketClient CreateClient(TcpClient client, Action<SocketConnectOptions> configure)
        {
            return new InternalSocketClient(client, configure);
        }
        #endregion
    }
}
